﻿using S7.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace thinger.VisionDemo
{
    public partial class Frm分料装置 : Form
    {
        Plc siemens;
        public Frm分料装置()
        {
            InitializeComponent();
        }

        private void btnPlcConnect_Click(object sender, EventArgs e)
        {
            try
            {


                siemens = new Plc(CpuType.S71200, txtPlcIP.Text, 102, 0, 1);


                // 连接对象
                siemens.Open();
                if (!siemens.IsConnected)
                {
                    labStatus.Text = "连接PLC失败！";
                }
                else
                {
                    labStatus.Text = "连接PLC成功！";
                    readInitialValue();
                }
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void readInitialValue()
        {

            //读取摇摆伺服模式设定
            ushort 烘箱温度设置 = (ushort)siemens.Read(DataType.DataBlock, 1, 406, VarType.Word, 1);
            //Console.WriteLine($"读取值: {摇摆伺服模式设定}");



            if (txt_烘箱温度设置.Text != 烘箱温度设置.ToString())
                txt_烘箱温度设置.Text = 烘箱温度设置.ToString();

        }


        private void btnPlcDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                siemens.Close();
                labStatus.Text = "PLC断开连接！";
                //updateTimer.Enabled = false;
            }
            catch
            {
                MessageBox.Show("参数错误，请检查！");
            }
        }

        private void btn_上升_Click(object sender, EventArgs e)
        {
            //siemens.Write("M102.6", true);
            siemens.WriteBit(DataType.DataBlock, 1, 102, 6, true);
            siemens.WriteBit(DataType.DataBlock, 1, 102, 6, false);
        }

        private void btn_下降_Click(object sender, EventArgs e)
        {
            //siemens.Write("M102.7", true);
            siemens.WriteBit(DataType.DataBlock, 1, 102, 7, true);
            siemens.WriteBit(DataType.DataBlock, 1, 102, 7, false);

        }

        private void btn_停止_Click(object sender, EventArgs e)
        {
            //siemens.Write("M103.0", true);
            siemens.WriteBit(DataType.DataBlock, 1, 103, 0, true);
            siemens.WriteBit(DataType.DataBlock, 1, 103, 0, false);

        }


        private void btn_频率运行设置_Click(object sender, EventArgs e)
        {
            ushort newValue = ushort.Parse(txt_运行频率设置.Text);
            siemens.Write(DataType.DataBlock, 1, 402, newValue);

        }

        private void btn_减速频率设置_Click(object sender, EventArgs e)
        {
            var value = short.Parse(txt_接近频率设置.Text);
            siemens.Write("MW404", value);

        }

        private void btn_震动器_1_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.0", true);

        }

        private void btn_震动器_2_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.1", true);
        }

        private void btn_震动器_3_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read("M100.2"))
            {
                siemens.Write("M100.2", false);

            }
            else
            {
               siemens.Write("M100.2", true);

            }


        }

        private void btn_震动器_4_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.3", true);

        }

        private void btn_震动器_5_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.4", true);

        }

        private void btn_震动器_6_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.5", true);

        }

        private void btn_震动器_7_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.6", true);

        }

        private void btn_震动器_8_Click(object sender, EventArgs e)
        {
            siemens.Write("M100.7", true);

        }

        private void btn_震动器_9_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.0", true);

        }

        private void btn_震动器_10_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.1", true);

        }

        private void btn_震动器_11_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.2", true);

        }

        private void btn_震动器_12_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.3", true);

        }

        private void btn_震动器_13_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.4", true);

        }

        private void btn_震动器_14_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.5", true);

        }

        private void btn_震动器_15_Click(object sender, EventArgs e)
        {
            siemens.Write("M101.6", true);

        }


        private void btn_前门开_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read(DataType.DataBlock, 1, 104, VarType.Bit, 1, 3))
            {
                siemens.WriteBit(DataType.DataBlock, 1, 104, 3, false);

            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 1, 104, 3, true);

            }

        }

        private void btn_后门开_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read(DataType.DataBlock, 1, 104, VarType.Bit, 1, 4))
            {
                siemens.WriteBit(DataType.DataBlock, 1, 104, 4, false);

            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 1, 104, 4, true);

            }
        }

        private void btn_烘箱温度设置_Click(object sender, EventArgs e)
        {
            ushort newValue = ushort.Parse(txt_烘箱温度设置.Text);
            siemens.Write(DataType.DataBlock, 1, 406, newValue);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read(DataType.DataBlock, 1, 103, VarType.Bit, 1, 3))
            {
                siemens.WriteBit(DataType.DataBlock, 1, 103, 3, false);

            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 1, 103, 3, true);

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if ((bool)siemens.Read(DataType.DataBlock, 1, 103, VarType.Bit, 1, 4))
            {
                siemens.WriteBit(DataType.DataBlock, 1, 103, 4, false);

            }
            else
            {
                siemens.WriteBit(DataType.DataBlock, 1, 103, 4, true);

            }
        }

        private void btn_烘箱温度显示_Click(object sender, EventArgs e)
        {
            txt_烘箱温度显示.Text = siemens.Read(DataType.DataBlock, 1, 408, VarType.Word, 1).ToString();
        }

        private void btn_料仓合_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 100, 0, true);
            siemens.WriteBit(DataType.DataBlock, 1, 100, 0, false);
        }

        private void btn_料仓合停止_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 104, 1, true);
            siemens.WriteBit(DataType.DataBlock, 1, 104, 1, false);
        }

        private void btn_料仓开_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 100, 3, true);
            siemens.WriteBit(DataType.DataBlock, 1, 100, 3, false);
        }

        private void btn_料仓开停止_Click(object sender, EventArgs e)
        {
            siemens.WriteBit(DataType.DataBlock, 1, 104, 2, true);
            siemens.WriteBit(DataType.DataBlock, 1, 104, 2, false);
        }

        private void tb_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmSoftKey.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmSoftKey frmSoftKey = new FrmSoftKey();
            // 计算窗体的位置
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y + tb_temp.Size.Height;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmSoftKey.confirm)
            {
                tb_temp.Text = FrmSoftKey.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
            }
        }

    }
}
